package com.kingwood.algorithm.test;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * https://fcqian.blog.csdn.net/article/details/134640254
 * 剩余银饰的重量
 */
public class ShengyuYinShiWeigh {
    static PriorityQueue<Integer> pq =new PriorityQueue<>((a, b) -> b - a);
    static int[] arr = new int[3];

    public static int getLastWeigh() {
        while (pq.size() >= 3) {
            for (int i=0; i<3; i++) {
                arr[i] = pq.poll();
            }

            int curLast = Math.abs((arr[2] - arr[1]) - (arr[1] - arr[0]));
            if (curLast > 0) {
                pq.offer(curLast);
            }

            Arrays.fill(arr, 0);
        }

        if (pq.size() == 1) {
            return pq.poll();
        }
        if (pq.size() == 2) {
            return Math.max(pq.poll(), pq.poll());
        }

        return 0;
    }

    public static void main(String[] args) {
//        int[] arr = {2,17,4,12,8,21,15,33};
//        for (int i : arr) {
//            pq.offer(i);
//        }
//
//        System.out.println(pq.size());
//        while(!pq.isEmpty()) {
//            int cur = pq.poll();
//            System.out.print(cur + " ");
//        }
//
//        System.out.println("\n" + pq.size());

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i=0; i<n; i++) {
            int weight = scanner.nextInt();
            pq.offer(weight);
        }

        System.out.println(getLastWeigh());
    }
}
